package com.hpin.mlxteacher.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.AddressBookResult;
import com.hpin.mlxteacher.chat.SearchContractActivity;
import com.hpin.mlxteacher.utils.CommonUtils;

import java.util.List;

/**
 * 我的通讯录适配器
 * Created by Aaron on 15/7/31.
 */
public class AddressBookAdapter extends BaseAdapter {

    private Context ctx;
    private List<AddressBookResult.Address> tempList;

    public AddressBookAdapter(Context ctx, List<AddressBookResult.Address> tempList) {
        this.ctx = ctx;
        this.tempList = tempList;
    }

    public void setTempList(List<AddressBookResult.Address> tempList) {
        this.tempList = tempList;
    }

    @Override
    public int getCount() {
        return tempList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_address_book, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.iv_send_message = (ImageView) convertView.findViewById(R.id.iv_send_message);
            viewHolder.iv_call = (ImageView) convertView.findViewById(R.id.iv_call);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_name.setText(tempList.get(position).nickName);
        viewHolder.tv_phone.setText(tempList.get(position).phoneNum);

        viewHolder.iv_send_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //发送好友申请
                Intent mIntent = new Intent(ctx, SearchContractActivity.class);
                mIntent.putExtra("contractNum", tempList.get(position).phoneNum);
//                mIntent.putExtra("sendName", tempList.get(position).nickName);
//                mIntent.putExtra("sendId", tempList.get(position).id);
//                mIntent.putExtra("isAdress", true);//是否是通讯录跳转到编辑信息页面的
                ctx.startActivity(mIntent);
            }
        });

        viewHolder.iv_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String phone = tempList.get(position).phoneNum;

                if (!CommonUtils.isPhone(phone)) {
                    Toast.makeText(ctx, "未获取到正确的手机号码!", Toast.LENGTH_SHORT).show();
                    return;
                }

                //拨打电话
                new AlertDialog.Builder(ctx).setMessage(phone).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //拨打电话
                        Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phone));
                        ctx.startActivity(intent);

                    }
                }).show();
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tv_name;
        private TextView tv_phone;
        private ImageView iv_send_message;
        private ImageView iv_call;

    }
}
